package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrInteger;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.nio.ByteBuffer;

public class ScalerUnit extends AxdrType {
    private byte[] dataCode = null;
    private AxdrInteger t1 = null;
    private AxdrEnum unit = null;

    public ScalerUnit() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public AxdrInteger getT1() {
        return t1;
    }

    public AxdrEnum getUnit() {
        return unit;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(AxdrInteger t1, AxdrEnum unit) {
        this.t1 = t1;
        this.unit = unit;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        return 0;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.unit != null && this.t1 != null) {
                codeLength = 0;
                codeLength += this.unit.encode(output);
                codeLength += this.t1.encode(output);
            } else {
                throw new RuntimeException("unit or t1 is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.unit != null && this.t1 != null) {
            return "sequence: {" + "t1: " + this.t1 + ", unit: " + this.unit + "}";
        } else {
            return "unit or t1 is null";
        }
    }
}



